//
//  VRStoryDetailViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/25.
//

import UIKit

class VRStoryDetailViewController: VRBaseViewController {
    
    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let moreButton          = c_make_ex(UIButton.self)
        .image("Discover_more").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
//        tableView.estimatedRowHeight = 200
        tableView.register(VRCommitTableViewCell.self,VRCommitDiscoverCell.self)
        return tableView
    }()
    
    private var commentList:[InfoUtil.commentDta] = []
    var itemModel: InfoUtil.room?{
        didSet{
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            moreButton
            tableView
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        moreButton.top(.statusHeight + 2).trailing(5).size(40)
        tableView.top(.navi_height).leading(0).trailing(0).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        moreButton.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
    }

    private func loadData(){
        
        RT.getCommentList(houseId: itemModel?.id ?? "") { list in
            self.commentList.removeAll()
            for item in list {
                if let it = item {
                    self.commentList.append(it)
                }
            }
//            if let delArr = UserDefaults.standard.value(forKey: self.itemModel.icon) {
//                let temp = delArr as! [String]
//                for del in temp {
//                    self.commentList.removeAll(where: {$0.id == del})
//                }
//            }
//            if self.commentList.count == 0 {
//                self.isCommentEmpty = true
//            }else {
//                self.isCommentEmpty = false
//            }
            self.tableView.reloadData()
        }
    }
}

extension VRStoryDetailViewController: UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 1 {
            return self.commentList.count
        }else{
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let cell = tableView.cell(VRCommitDiscoverCell.self, indexPath: indexPath)
            cell.roomModel = itemModel
            return cell
        }else{
            let cell = tableView.cell(VRCommitTableViewCell.self, indexPath: indexPath)
            cell.model = self.commentList[indexPath.row]
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
//        let detailVC = VRStoryDetailViewController()
//
//        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 430
        }else{
            return 80
        }
    }
}

//MARK: action
extension VRStoryDetailViewController {
    
    @objc private func clickMoreButton(){
        
    }
}
